Ext.define('Admin.view.main.Main', {
    extend: 'Ext.container.Viewport',

    requires: [
        'Ext.button.Segmented',
        'Ext.list.Tree'
    ],

    controller: 'main',
    viewModel: 'main',

    cls: 'sencha-dash-viewport',
    itemId: 'mainView',

    layout: {
        type: 'vbox',
        align: 'stretch'
    },

    listeners: {
        render: 'onMainViewRender'
    },

    items: [{
        xtype: 'toolbar',
        cls: 'sencha-dash-dash-headerbar shadow',
        height: 64,
        itemId: 'headerBar',
        items: [{
            xtype: 'component',
            reference: 'senchaLogo',
            cls: 'sencha-logo',
            html: '<div class="main-logo"><img src="resources/images/company-logo.png">办公自动化系统</div>',
            width: 250
        },
        {
            margin: '0 0 0 8',
            ui: 'header',
            iconCls: 'x-fa fa-navicon',
            id: 'main-navigation-btn',
            handler: 'onToggleNavigationSize'
        },
            '->',
        {
            xtype: 'tbtext',
            text: fullName,
            cls: 'top-user-name'
        },
        {
            xtype: 'image',
            id: 'mainCover',
            cls: 'header-right-profile-image',
            height: 35,
            width: 35,
            alt: 'current user image',
            listeners: {
                afterrender: function () {
                    var data = Ext.util.JSON.decode(userInfo);
                    if (data != null) {
                        this.setSrc("upload/" + data.cover);
                    }
                }
            }
        }, {
            iconCls: 'x-fa fa-sign-out',
            ui: 'header',
            tooltip: '登出系统',
            handler: 'logoutButton'
        }
        ]
    },
    {
        xtype: 'maincontainerwrap',
        id: 'main-view-detail-wrap',
        reference: 'mainContainerWrap',
        flex: 1,
        items: [{
            xtype: 'container',
            scrollable: 'y',
            region: 'west',
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [{
                xtype: 'treelist',
                reference: 'navigationTreeList',
                itemId: 'navigationTreeList',
                ui: 'nav',
                store: 'NavigationTree',
                minHeight: Ext.Element.getViewportHeight(),
                width: 250,
                expanderFirst: false,
                expanderOnly: false,
                listeners: {
                    selectionchange: 'onNavigationTreeSelectionChange',
                }
            }]
        },
        {
            xtype: 'container',
            flex: 1,
            reference: 'mainCardPanel',
            region: 'center',
            cls: 'sencha-dash-right-main-container',
            itemId: 'contentPanel',
            layout: {
                type: 'card',
                anchor: '100%'
            }
        }
        ]
    }
    ],

    listeners: {
        afterrender: function () {
            var navigationList = this.lookupReference('navigationTreeList'),
                data = Ext.util.JSON.decode(userInfo),
                groups = [];

            //根据权限设置树的视图
            if (data != null && data.group != null) {
                Ext.each(data.group, function (item) {
                    groups.push(item.name);
                });

                if (contains(groups, 'ADMIN') || contains(groups, 'HIGH_MANAGER')) {
                    navigationList.setStore('Admin.store.NavigationTree');
                } else {
                    if (contains(groups, 'HR') || contains(groups, 'MANAGER')) {
                        var hrTree = Ext.create('Admin.store.NavigationHRTree');
                        navigationList.setStore(hrTree);
                    } else {
                        if (contains(groups, 'DEFEND')) {
                            var defendTree = Ext.create('Admin.store.NavigationDefendTree');
                            navigationList.setStore(defendTree);
                        } else {
                            var normalTree = Ext.create('Admin.store.NavigationNormalTree');
                            navigationList.setStore(normalTree);
                        }
                    }
                }
            }
        }
    }
});

function contains(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return true;
        }
    }
    return false;
}